// pages/addDemand/addDemand.js
// pages/addSupply/addSupply.js
var upload = require('../../utils/upload.js');
var validate = require('../../utils/validate.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    host:getApp().globalData.host,
    item:{},
    units:['千克','吨','饼','克','件','提'],
    unitIndex:0,
    images:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.item.unit = this.data.units[this.data.unitIndex];
    this.setData({
       item: this.data.item
    })
  },
  bindTitle:function(e){
    this.data.item.title = e.detail.value;
    this.setData({
       item: this.data.item
    })
  },
  bindDesc:function(e){
    this.data.item.desc = e.detail.value;
    this.setData({
       item: this.data.item
    })
  },
  bindTypeName:function(e){
    this.data.item.typeName = e.detail.value;
    this.setData({
       item: this.data.item
    })
  },
  bindAreaName:function(e){
    this.data.item.producingAreaName = e.detail.value;
    this.setData({
       item: this.data.item
    })
  },
  bindYear:function(e){
    this.data.item.producingYear = e.detail.value;
    this.setData({
       item: this.data.item
    })
  },
  bindShapeName:function(e){
    this.data.item.shapeName = e.detail.value;
    this.setData({
       item: this.data.item
    })
  },
  bindSpec:function(e){
    this.data.item.spec = e.detail.value;
    this.setData({
       item: this.data.item
    })
  },
  bindQuantity:function(e){
    this.data.item.quantity = e.detail.value;
    this.setData({
       item: this.data.item
    })
  },
  bindUnitChange:function(e){
    this.data.item.unit = this.data.units[e.detail.value];
    this.setData({
       item: this.data.item,
       unitIndex:e.detail.value
    })
  },
  bindMinAmount:function(e){
    this.data.item.minAmount = e.detail.value;
    this.setData({
       item: this.data.item
    })
  },
  bindMaxAmount:function(e){
    this.data.item.maxAmount = e.detail.value;
    this.setData({
       item: this.data.item
    })
  },
  bindArea:function(e){
    this.data.item.destArea = e.detail.value;
    this.setData({
       item: this.data.item
    })
  },
  bindContact:function(e){
    this.data.item.contactUserName = e.detail.value;
    this.setData({
       item: this.data.item
    })
  },
  bindMobile:function(e){
    this.data.item.contactMobile = e.detail.value;
    this.setData({
       item: this.data.item
    })
  },
  //上传图片
  uploadImg:function(){
    //upload.uploadImg(this,'uploadFinish');
    upload.uploadFrontImg(this,'uploadFinish');
  },
  uploadFinish:function(attachment){
    if(typeof(attachment)!='undefined'){
      this.data.images.push(attachment);
      this.setData({
        images: this.data.images
      })
    }
  },
  //发布信息
  doAction:function(){
    var that = this;
    if(!validate.validateEmpty(this.data.item.title,'标题信息不得为空'))
      return;
    if(!validate.validateEmpty(this.data.item.desc,'商品描述不得为空'))
      return;
    if(!validate.validateInteger(this.data.item.quantity,0,'商品数量不正确(>0整数)'))
      return;
    if(!validate.validateInteger(this.data.item.minAmount,0,'最低总额不正确(>0整数)'))
      return;
    if(!validate.validateInteger(this.data.item.maxAmount,0,'最高总额不正确(>0整数)'))
      return;
    if(!validate.validateEmpty(this.data.item.destArea,'收货地不得为空'))
      return;
    if(!validate.validateEmpty(this.data.item.typeName,'茶类不得为空'))
      return;
    if(!validate.validateEmpty(this.data.item.contactUserName,'联系人姓名不得为空'))
      return;
    if(!validate.validateMobile(this.data.item.contactMobile))
      return;
    if(!validate.validateInteger(this.data.item.producingYear,0,'年份信息不正确(>0整数)'))
      return;
    
    var imgIds = '';
    var regexp = /[a-zA-Z0-9]{32}/gi;
    for(var i = 0; i<this.data.images.length;i++){
      imgIds += this.data.images[i].match(regexp)[0] + ',';
    }
    if(imgIds.length>1)
      this.data.item.imageIds = imgIds.substring(0,imgIds.length-1);
    wx.request({
      url: getApp().globalData.host + 'demand/publish.html',      
      data: JSON.stringify(that.data.item),
      header: getApp().globalData.jsonHeader,
      method:'POST',
      success: function (res) {
        if(res.data.errorCode==0){
          wx.showToast({
            title: "信息已提交审核，请耐心等待！",
            duration: 2000
          })
          //延迟一秒返回前页（显示提示信息）
          setTimeout(function () {
            wx.navigateBack();
          }, 1500)
        }else{
          wx.showToast({
            title: "发布失败," + res.data.errorMsg,
            duration: 2000
          })
        }
      },
      fail: function (res) { },
      complete: function (res) { }
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})